package com.salmon.oss.thread;

import com.salmon.oss.entity.GoFastDfsUploadResult;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.impl.Iq80DBFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class InsertLevelDB implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(InsertLevelDB.class);
    private DB db;
    private byte[] md5;
    private byte[] fileUrl;

    public InsertLevelDB(DB db, Object result) {
        if (result instanceof GoFastDfsUploadResult) {
            GoFastDfsUploadResult go = (GoFastDfsUploadResult) result;
            this.db = db;
            this.md5 = Iq80DBFactory.bytes(go.getMd5());
            this.fileUrl = Iq80DBFactory.bytes(go.getUrl());
        }
    }

    public InsertLevelDB(DB db, String md5, String url) {
        this.db = db;
        this.md5 = Iq80DBFactory.bytes(md5);
        this.fileUrl = Iq80DBFactory.bytes(url);
    }

    public void run() {
        if (this.db != null) {
            this.db.put(this.md5, this.fileUrl);
            log.debug("进行数据入库md5是:{}，地址是:{}", Iq80DBFactory.asString(this.md5), Iq80DBFactory.asString(this.fileUrl));
        }
    }
}